package com.apes.autoparts.parts.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.parts.repository.PartDao;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.MapUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Part.CONST_TABLE_NAME)
public class Part extends Model {
    public static final String CONST_TABLE_NAME = "auto_part";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车型属性")
    @Comment("唯一码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "org_selector_id", referencedColumnName = "id")
    @Comment("车型")
    private AutoModel orgSelectorId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "main_group_id", referencedColumnName = "id")
    @Comment("主组")
    private MainGroup mainGroupId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sub_group_id", referencedColumnName = "id")
    @Comment("子组")
    private SubGroup subGroupId;

    @Column(name = "part_id")
    @Comment("第三方配件唯一码")
    private String partId;

    @Column(name = "part_code")
    @Comment("零件编号")
    private String partCode;

    @Column(name = "part_name", length = 4000)
    @Comment("零件名称")
    private String partName;

    @Column(name = "image_sn")
    @Comment("图号")
    private String imageSN;

    @Column(name = "price", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("销售价")
    private double price = 0;

    @Column(name = "cost", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("成本价(进价)")
    private double cost = 0;

    @Column(name = "price_updated_at")
    @Comment("价格更新日期")
    private Date priceUpdatedAt;

    @Column(name = "main_group_code")
    @Comment("主组编号")
    private String mainGroupCode;

    @Column(name = "sub_group_code")
    @Comment("子组编号")
    private String subGroupCode;

    @Column(name = "belong_to_vin", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("属于此vin")
    private boolean belongToVin = true;

    @Column(name = "description")
    @Comment("描述")
    private String description;

    @Column(name = "note", length = 4000)
    @Comment("备注")
    private String note;

    @Column(name = "quantity")
    @Comment("单车用量")
    private String quantity;

    @Column(name = "model_notes")
    @Comment("车型")
    private String modelNotes;

    @Column(name = "model_name")
    @Comment("车型")
    private String modelName;

    @Column(name = "brand_code")
    @Comment("品牌代码")
    private String brandCode;

    @Column(name = "brand_name")
    @Comment("品牌名称")
    private String brandName;

    @Column(name = "first_year")
    @Comment("开始年份")
    private int firstYear;

    @Column(name = "last_year")
    @Comment("结束年份")
    private int lastYear;

    @Column(name = "replaced_part_codes", length = 4000)
    @Comment("替换件")
    private String replacedPartCodes;

    @Column(name = "selector_Id")
    @Comment("车型")
    private String selectorId;

    @Column(name = "market_name")
    @Comment("市场名称")
    private String marketName;

    @Column(name = "market_code")
    @Comment("市场编码")
    private String marketCode;

    @Transient
    private String realSelectorId;

    public String getRealSelectorId() {
        return this.selectorId != null ? this.selectorId : this.orgSelectorId.getSelectorId();
    }


    @Column(name = "timestamp")
    @Comment("时间戳")
    private Date timestamp;
    /***
     * oe所对应的配件名称
     */
//    @Transient
//    List<Map> categories;
//
//    public List<Map> getCategories() {
//        if (this.partCode == null || this.orgSelectorId.getQCCPMC() == null) return new ArrayList<>();
//        JSONArray tempArray = SpringManager.getBean(PartDao.class).queryCategories(this.partCode, this.orgSelectorId.getQCCPMC());
//
//        if (tempArray.size() == 0) return new ArrayList<>();
//
//        return tempArray.stream().map(obj -> {
//            JSONObject item = (JSONObject) obj;
//            return MapUtil.mapper(
//                    "id", item.getString("PJMCID"),
//                    "name", item.getString("PJMC")
//            );
//        }).collect(Collectors.toList());
//    }


}
